package com.jiuyuejiu.func_video.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.R;
import com.jiuyuejiu.func_video.domain.VideoDetailStc;

import java.util.List;

/**
 * Created by y
 * On 2016/02/07 01:20
 */
public class VideoRandomAdapter extends BaseAdapter {

    public static final String TAG = "VideoHaopingAdapter";

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            // .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_gray_5)// 占位图
            .transform(new CenterCrop(), new RoundedCorners(18));

    private Context context;
    private List<VideoDetailStc> lst;

    // 宽高参数
    private RelativeLayout.LayoutParams linearParams;

    public VideoRandomAdapter(Context context, List<VideoDetailStc> lst) {
        this.context = context;
        this.lst = lst;

        // ----↓ 图片高度 2022/6/4 17:46 ↓------------------------------------------------------------------------------------
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        // int screenWidth = (int) (dm.widthPixels );
        int screenWidth = (int)(dm.widthPixels/3);
        int screenHeight = (int) (screenWidth / 2);
        linearParams = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
        // ----↑ 图片高度 2022/6/4 17:46 ↑------------------------------------------------------------------------------------
    }

    @Override
    public int getCount() {
        return lst.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);

            convertView = mInflater.inflate(R.layout.item_home_random_video, null);
            viewHolder.squenTv = (TextView) convertView.findViewById(R.id.item_home_random_tv_squen);
            viewHolder.titleTv = (TextView) convertView.findViewById(R.id.item_home_random_tv_title);
            viewHolder.fengmianImg = (ImageView) convertView.findViewById(R.id.item_home_random_img_fengmian);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        VideoDetailStc stc = lst.get(position);
        // 序号
        viewHolder.squenTv.setText("[ " + (stc.getCategoryposition() + 1) + " , " + (stc.getVideoposition() + 1) + " ] " + stc.getCategory());
        // 名称
        viewHolder.titleTv.setText(stc.getVideoname());

        viewHolder.fengmianImg.setLayoutParams(linearParams);
        Glide.with(convertView.getContext())
                // .setDefaultRequestOptions(BANNER_OPTIONS)
                .load(stc.getImageurl())
                .apply(BANNER_OPTIONS)
                .into(viewHolder.fengmianImg);

        return convertView;
    }

    class ViewHolder {
        TextView squenTv;
        TextView titleTv;
        ImageView fengmianImg;
    }
}
